package com.larly.controller;


import com.larly.common.BaseResponse;
import com.larly.common.IdRequest;
import com.larly.common.ResultUtils;
import com.larly.exception.ErrorCode;
import com.larly.exception.ThrowUtils;
import com.larly.model.domain.InteractionLike;
import com.larly.model.domain.NoteLike;
import com.larly.model.vo.InteractionsLikeVO;
import com.larly.model.vo.NotesLikeVO;
import com.larly.service.InteractionLikeService;
import com.larly.service.NoteLikeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/note_like")
public class NoteLikeController {

    @Resource
    private NoteLikeService noteLikeService;

    @PostMapping("/list")
    public BaseResponse<NotesLikeVO> list(@RequestBody IdRequest idRequest) {
        ThrowUtils.throwIf(idRequest == null, ErrorCode.PARAM_ERROR);
        Integer id = idRequest.getId();
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAM_ERROR);
        List<Integer> userIds = noteLikeService.lambdaQuery()
                .eq(NoteLike::getNoteId, id)
                .select(NoteLike::getUserId)
                .list()
                .stream()
                .map(NoteLike::getUserId)
                .collect(Collectors.toList());
        NotesLikeVO result = new NotesLikeVO();
        result.setUserIds(userIds);
        return ResultUtils.success(result);
    }

    /**
     * 获取笔记id列表根据用户id用于展示能不能点赞
     */
    @PostMapping("/like")
    public BaseResponse<NotesLikeVO> listNote(@RequestBody IdRequest idRequest) {
        ThrowUtils.throwIf(idRequest == null, ErrorCode.PARAM_ERROR);
        Integer id = idRequest.getId();
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAM_ERROR);
        List<Integer> noteIds = noteLikeService.lambdaQuery()
                .eq(NoteLike::getUserId, id)
                .select(NoteLike::getNoteId)
                .list()
                .stream()
                .map(NoteLike::getNoteId)
                .collect(Collectors.toList());
        NotesLikeVO result = new NotesLikeVO();
        result.setNoteIds(noteIds);
        return ResultUtils.success(result);
    }

}
